package io.israel.ephraim;

import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.BpmnError;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Optional;

/**
 * @author lijinting01
 */
@Component
public class InsureProcessDelegate implements JavaDelegate {

	private RuntimeService runtimeService;

	@Override
	public void execute(DelegateExecution execution) {
		String processDefinitionKey = Optional.ofNullable(execution.getVariable("processDefinitionKey", String.class))
				.orElseThrow(() -> new BpmnError("E3366", "Process Definition Key Not Found"));
		runtimeService.startProcessInstanceByKey(processDefinitionKey, Collections.singletonMap("processDefinitionKey", processDefinitionKey));
	}

	@Autowired
	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}
}
